import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:untitled1/app/res/myColors.dart';
import 'package:untitled1/app/utils/utilRouter.dart';


import '../../../widgets/base_page/BaseStatefulPage.dart';
import '../../../widgets/toast/MyToast.dart';
import 'OrderPageController.dart';
import 'OrderDetailPage.dart';

//订单列表
class OrderPage extends BaseStatefulPage {
  const OrderPage({super.key}) : super(pageName: "首页");

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() => _OrderPageState();
}

class _OrderPageState extends BaseStatefulPageState<OrderPage> {
  String? _error;
  bool _isEmpty = false;
  final OrderPageController controller = Get.put(OrderPageController());

  @override
  void initState() {
    super.initState();
    // 页面初始化，加载数据
    loadData();
  }

  void loadData() async {
    safeSetState(() {
      _error = null;
      _isEmpty = false;
    });

    await Future.delayed(const Duration(seconds: 2));

    // 模拟异常或者空数据
    safeSetState(() {
      _error = null; // "网络异常";
      _isEmpty = false;
    });
  }

  @override
  Widget buildPage(BuildContext context) {
    if (_error != null) {
      return buildErrorView(_error);
    }
    if (_isEmpty) {
      return buildEmptyView();
    }
    return Scaffold(
      body: Obx(
        () => Container(
          width: double.infinity,
          height: double.infinity,
          color: myColors.white,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              SizedBox(height: 50, width: double.infinity),
              Container(
                height: 50,
                width: double.infinity,
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    TextButton(
                      onPressed: () => titleClick(0),
                      child: Text("全部", style: getViewTitleStyle(0)),
                    ),
                    TextButton(
                      onPressed: () => titleClick(1),
                      child: Text("待支付", style: getViewTitleStyle(1)),
                    ),
                    TextButton(
                      onPressed: () => titleClick(2),
                      child: Text("未付清", style: getViewTitleStyle(2)),
                    ),
                    TextButton(
                      onPressed: () => titleClick(3),
                      child: Text("已付清", style: getViewTitleStyle(3)),
                    ),
                    TextButton(
                      onPressed: () => titleClick(4),
                      child: Text("已退款", style: getViewTitleStyle(4)),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: Container(
                  child: Obx(
                    () => IndexedStack(
                      sizing: StackFit.expand,
                      index: controller.page_index.value,
                      children: [
                        MyListView(type: 1),
                        MyListView(type: 2),
                        MyListView(type: 3),
                        MyListView(type: 1),
                        MyListView(type: 5),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void titleClick(int index) {
    controller.page_index.value = index;
    // controller.title_select_index.value = index; // 更新选中状态
    print(">]aaaaaa");
  }

  TextStyle getViewTitleStyle(int index) {
    if (index == controller.page_index.value) {
      return TextStyle(
        color: myColors.c_191919,
        fontWeight: FontWeight.bold,
        fontSize: 16,
      );
    }
    return TextStyle(
      color: myColors.c_707070,
      fontWeight: FontWeight.bold,
      fontSize: 14,
    );
  }
}

class MyListView extends BaseStatefulPage {
  int type = -1;

  MyListView({super.key, required this.type}) : super(pageName: "首页");

  @override
  BaseStatefulPageState<BaseStatefulPage> createState() => _MyListViewState();
}

class _MyListViewState extends BaseStatefulPageState<MyListView> {
  @override
  Widget buildPage(BuildContext context) {
    return ListView.separated(
      itemCount: 5,
      //widget.size,//20,
      // 总共显示 20 项
      scrollDirection: Axis.vertical,
      // 列表垂直滚动
      reverse: false,
      // 是否倒序，false 表示从上往下滚动
      controller: ScrollController(),
      // 控制滚动位置（非必须）
      primary: false,
      // 如果为 true，不允许同时设置 controller
      physics: const BouncingScrollPhysics(),
      // iOS 回弹效果
      shrinkWrap: false,
      // false 表示撑满父容器；嵌套场景设为 true
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      // 整体内边距
      keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
      separatorBuilder: (BuildContext context, int index) {
        return Container(height: 10, color: myColors.c_FBFBFB);
      },
      // 滚动时关闭键盘
      itemBuilder: (context, index) {
        if(widget.type==1||widget.type==2||widget.type==3){
          return buildItem1(context, index);
        }
       return buildItem2(context, index);
      },
    );
  }

  Widget buildItem1(BuildContext context, int index) {
    return InkWell(
      onTap: () {
       itemClick(index);
      },
      child:Container(
        padding: EdgeInsets.only(top: 10, bottom: 10),
        child: Row(
          children: [
            Expanded(
              flex: 7,
              child: Container(
                padding: EdgeInsets.only(left: 15, right: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "订单编号：20231001xxxxxx",
                      style: TextStyle(fontSize: 14, color: myColors.c_191919),
                    ),
                    SizedBox(height: 8),
                    // getFwDingjin(index),
                    Text(
                      "服务费定金",
                      style: TextStyle(fontSize: 14, color: myColors.c_191919),
                    ),
                    SizedBox(height: 8),
                    Text(
                      "2025.06.30 17:45:34",
                      style: TextStyle(fontSize: 14, color: myColors.c_191919),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Container(
                child: Column(
                  children: [
                    Text(
                      "待支付1000元",
                      style: TextStyle(
                        color: myColors.c_B51C21,
                        fontWeight: FontWeight.bold,
                        fontSize: 14,
                      ),
                    ),
                    SizedBox(height: 5),
                    GestureDetector(
                      onTap: () {
                        MyToast.showSystemToast("aaa");
                      },
                      child: Container(
                        alignment: Alignment.center,
                        height: 25,
                        width: 100 ?? MediaQuery.of(context).size.width - 30,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(20),
                          gradient: LinearGradient(
                            colors:[Color(0xFFF89596), Color(0xFFFF4142)], // 默认渐变
                            begin: Alignment.centerLeft,
                            end: Alignment.centerRight,
                          ),
                        ),
                        child: Text(
                          '缴纳服务费',
                          style: TextStyle(color: myColors.white, fontSize: 14),
                        ),
                      ),
                    ),

                  ],
                ),
              ),
            ),
          ],
        ),
      ) ,
    );
  }
  Widget buildItem2(BuildContext context, int index) {
    return InkWell(
        onTap: () {
          itemClick(index);
    },
    child: Container(
      padding: EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        children: [
          Expanded(
            flex: 7,
            child: Container(
              padding: EdgeInsets.only(left: 15, right: 15),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "订单编号：20231001xxxxxx",
                    style: TextStyle(fontSize: 14, color: myColors.c_191919),
                  ),
                  SizedBox(height: 8),
                  // getFwDingjin(index),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text(
                          "家庭陪伴",
                          style: TextStyle(fontSize: 14, color: myColors.c_191919),
                        ),
                        SizedBox(width: 8),
                        Text('|'),
                        SizedBox(width: 8),
                        Text("张先生", style: TextStyle(fontSize: 14, color: myColors.c_191919)),
                        SizedBox(width: 8),
                        Text('|'),
                        SizedBox(width: 8),
                        Text(
                          "薪资代收",
                          style: TextStyle(fontSize: 14, color: myColors.c_191919),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 8),
                  Text(
                    "2025.06.30 17:45:34",
                    style: TextStyle(fontSize: 14, color: myColors.c_191919),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 3,
            child: Text(
              "已退款订单金额",
              style: TextStyle(
                color: myColors.c_B51C21,
                fontWeight: FontWeight.bold,
                fontSize: 14,
              ),
            ),
          ),
        ],
      ),
    ));
  }
  void itemClick(int index){
    utilRouter.to(OrderDetailPage());
  }

}
